package com.toy.core.security.context;

import org.springframework.util.Assert;


/**
 * A <code>ThreadLocal</code>-based implementation of {@link
 * org.acegisecurity.context.SecurityContextHolderStrategy}.
 *
 * @author Ben Alex
 * @version $Id: SecurityContextHolder.java 1324 2006-02-12 06:29:53Z benalex $
 *
 * @see java.lang.ThreadLocal
 * @see org.acegisecurity.context.HttpSessionContextIntegrationFilter
 */
public class ThreadLocalSecurityContextHolderStrategy implements SecurityContextHolderStrategy {
    //~ Static fields/initializers =====================================================================================

    private static ThreadLocal<SecurityContext> contextHolder = new ThreadLocal<SecurityContext>();

    //~ Methods ========================================================================================================

    public void clearContext() {
        contextHolder.set(null);
    }

    public SecurityContext getContext() {
        if (contextHolder.get() == null) {
            contextHolder.set(new SecurityContextImpl());
        }

        return contextHolder.get();
    }

    public void setContext(SecurityContext context) {
        Assert.notNull(context, "Only non-null SecurityContext instances are permitted");
        contextHolder.set(context);
    }
}